package com.itheima.api;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.PmsBrandMapper;
import com.itheima.mapper.PmsProductMapper;
import com.itheima.pojo.db.PmsBrand;
import com.itheima.pojo.db.PmsProduct;
import com.itheima.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class PmsBrandApiImpl implements PmsBrandApi {
    @Autowired
    private PmsBrandMapper pmsBrandMapper;

    @Autowired
    private PmsProductMapper pmsProductMapper;

    /**
     * 添加品牌
     *
     * @param pmsBrand
     * @return
     */
    @Override
    public boolean createBrand(PmsBrand pmsBrand) {
        pmsBrandMapper.insert(pmsBrand);
        return pmsBrand.getId() == null ? false : true;
    }


    /**
     * 批量删除
     *
     * @param ids
     */
    @Transactional
    public void delete(List<Integer> ids) {
        List<PmsProduct> pmsProducts = new ArrayList<>();
        for (Integer brandId : ids) {
            PmsProduct pmsProduct = new PmsProduct();
            pmsProduct.setBrandId(brandId.longValue());
            pmsProduct.setDeleteStatus(1);
            pmsProducts.add(pmsProduct);
        }

        for (PmsProduct pmsProduct : pmsProducts) {
            LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
            lqw.eq(PmsProduct::getBrandId, pmsProduct.getBrandId());
            pmsProductMapper.update(pmsProduct, lqw);
        }

        pmsBrandMapper.deleteBatchIds(ids);

    }

    /**
     * 单个删除
     *
     * @param id
     */
    @Transactional
    public void deleteById(Integer id) {
        PmsProduct pmsProduct = new PmsProduct();
        pmsProduct.setBrandId(id.longValue());
        pmsProduct.setDeleteStatus(1);
        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PmsProduct::getBrandId, pmsProduct.getBrandId());

        pmsProductMapper.update(pmsProduct, lqw);
        pmsBrandMapper.deleteById(id);

    }

    /**
     * 根据条件分页查询品牌
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @Override
    public PageResult<PmsBrand> pageByCondition(Integer pageNum, Integer pageSize, String keyword) {
        LambdaQueryWrapper<PmsBrand> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(keyword), PmsBrand::getName, keyword);

        //得到按条件分页对象
        IPage<PmsBrand> iPage = new Page<>(pageNum, pageSize);
        IPage<PmsBrand> iPages = pmsBrandMapper.selectPage(iPage, lqw);


        PageResult<PmsBrand> page = new PageResult<>();
        page.setList(iPages.getRecords());
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        page.setTotalPage(new Long(iPages.getPages()).intValue());
        page.setTotal(new Long(iPages.getTotal()).intValue());
        return page;
    }

    /**
     * 查询所有品牌
     *
     * @return
     */
    @Override
    public List<PmsBrand> listAll() {
        List<PmsBrand> pmsBrands = pmsBrandMapper.selectList(new LambdaQueryWrapper<PmsBrand>());
        return pmsBrands;
    }


    /**
     * 批量修改厂家制造商状态
     *
     * @param factoryStatus
     * @param ids
     */
    @Override
    public void upateFactoryStatus(Integer factoryStatus, List<Integer> ids) {
        for (Integer id : ids) {
            PmsBrand pmsBrand = new PmsBrand();
            pmsBrand.setFactoryStatus(factoryStatus);
            pmsBrand.setId(id.longValue());
            pmsBrandMapper.updateById(pmsBrand);
        }
    }

    /**
     * 批量修改显示状态
     *
     * @param showStatus
     * @param ids
     */
    @Override
    public void updateShowStatus(Integer showStatus, List<Integer> ids) {
        for (Integer id : ids) {
            PmsBrand pmsBrand = new PmsBrand();
            pmsBrand.setId(id.longValue());
            pmsBrand.setShowStatus(showStatus);
            pmsBrandMapper.updateById(pmsBrand);
        }
    }

    /**
     * 需改单个品牌
     *
     * @param pmsBrand
     */
    @Override
    public void updateById(PmsBrand pmsBrand) {
        pmsBrandMapper.updateById(pmsBrand);
    }

    /**
     * 根据ID查询
     *
     * @param id
     */
    @Override
    public PmsBrand findById(long id) {
        return pmsBrandMapper.selectById(id);
    }
}
